package table

import (
	"fmt"
	"goProject/db"
)

func init() {
	_ = db.MySQL.AutoMigrate(&MenuDir{})
	_ = db.MySQL.AutoMigrate(&MenuItem{})

	//删除掉注册请求列表
	db.MySQL.Exec("DROP TABLE IF EXISTS " + fmt.Sprint(MenuItemURL{}.TableName()))
	_ = db.MySQL.AutoMigrate(&MenuItemURL{})

	db.MySQL.FirstOrCreate(&MenuDir{ID: 1, Path: "root", Icon: "secured", Label: "ROOT管理"})
	db.MySQL.FirstOrCreate(&MenuItem{ID: 1, MenuDirID: 1, Path: "config", Icon: "setting", Label: "系统设置", Title: "系统设置"})
	db.MySQL.FirstOrCreate(&MenuItem{ID: 2, MenuDirID: 1, Path: "role", Icon: "user-avatar", Label: "角色管理", Title: "角色管理"})
	db.MySQL.FirstOrCreate(&MenuItem{ID: 3, MenuDirID: 1, Path: "status", Icon: "chart", Label: "程序状态", Title: "程序状态"})

	db.MySQL.FirstOrCreate(&MenuDir{ID: 2, Path: "admin", Icon: "service", Label: "Admin管理"})
	db.MySQL.FirstOrCreate(&MenuItem{ID: 100, MenuDirID: 2, Path: "user", Icon: "usergroup", Label: "用户管理", Title: "用户管理"})
	db.MySQL.FirstOrCreate(&MenuItem{ID: 101, MenuDirID: 2, Path: "code", Icon: "user-add", Label: "新注册码", Title: "新注册码"})
	db.MySQL.FirstOrCreate(&MenuItem{ID: 102, MenuDirID: 2, Path: "product", Icon: "cart", Label: "商品管理", Title: "商品管理"})
	db.MySQL.FirstOrCreate(&MenuItem{ID: 103, MenuDirID: 2, Path: "openai", Icon: "search", Label: "OpenAI", Title: "OpenAI"})
}

// MenuDir 菜单折叠项
type MenuDir struct {
	ID    uint       `json:"id" gorm:"primarykey"`
	Path  string     `json:"path" gorm:"type:varchar(50)"`  //页面路径
	Icon  string     `json:"icon" gorm:"type:varchar(50)"`  //菜单前面的图标
	Label string     `json:"label" gorm:"type:varchar(50)"` //菜单名字
	Items []MenuItem `json:"items"`                         //子集项列表
	Sort  uint       `json:"sort"`                          //排序编号
}

func (MenuDir) TableName() string {
	return "menu_dir"
}

// MenuItem 菜单实际页面
type MenuItem struct {
	ID        uint   `json:"id" gorm:"primarykey"`
	MenuDirID uint   `json:"menu_dir_id"`
	Path      string `json:"path" gorm:"type:varchar(50)"`  //页面路径
	Icon      string `json:"icon" gorm:"type:varchar(50)"`  //菜单前面的图标
	Label     string `json:"label" gorm:"type:varchar(50)"` //菜单名字

	Title string `json:"title" gorm:"type:varchar(255)"` //页面标题名字
	Sort  uint   `json:"sort"`                           //排序编号

	URL []MenuItemURL `json:"-"` //拥有的请求

	RoleMenuItem *bool `json:"role_menu_item,omitempty" gorm:"-"` //角色权限是否拥有该权限
}

func (MenuItem) TableName() string {
	return "menu_item"
}

// MenuItemURL 菜单实际页面，包含的请求信息
type MenuItemURL struct {
	ID         uint   `json:"id" gorm:"primarykey"`
	Path       string `json:"path" gorm:"index:path_method,unique;type:varchar(100);"`   //请求路径
	Method     string `json:"method" gorm:"index:path_method,unique;type:varchar(100);"` //请求方法
	MenuItemID uint   `json:"menu_item_id"`                                              //所属的页面ID，菜单ID
}

func (MenuItemURL) TableName() string {
	return "menu_item_url"
}
